﻿using IOP.Extension.DependencyInjection;
using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample101
{
    public class InputSystem : ComponentSystem
    {
        [Autowired]
        private IGraphicsManager GraphicsManager;
        private Camera Camera;
        private Area Area;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public InputSystem()
        {
        }

        public override void Initialization()
        {
            GraphicsManager.TryGetCamera("main", out Camera camera);
            Camera = camera;
            var viewPort = Camera.Viewport;
            Camera.SetScissor(viewPort.Width / 4, viewPort.Height / 4, viewPort.Width / 2, viewPort.Height / 2);
        }

        public override void Update(TimeSpan timeSpan)
        {

            if (Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Press)
            {
                var delta = Input.GetMousePositionDelta();
                if (delta.X != 0 || delta.Y != 0)
                {
                    Camera.EncircleFromMouse(Vector3.Zero, delta.X, delta.Y, (float)(0.2 * Math.PI / 180.0));
                    EventBus.Publish("CameraChanged", Camera, EventTrigger.SystemFrameAfter);
                }
            }
            else if(Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Click)
            {
                int x, y;
                var p = Input.GetMousePosition();
                var viewPort = Camera.Viewport;
                x = (int)(p.X);
                y = (int)(p.Y);
                if (x > viewPort.Width / 2) Area.X = (int)(viewPort.Width / 2);
                else Area.X = x;
                if (y > viewPort.Height / 2) Area.Y = (int)(viewPort.Height / 2);
                else Area.Y = y;
                Camera.SetScissor(Area.X, Area.Y, viewPort.Width / 2, viewPort.Height / 2);
            }
        }
    }
}
